//
//  GQHHomeView.m
//  Xanthium
//
//  Created by GuanQinghao on 24/02/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHHomeView.h"
#import "GQHHeader.h"
#import "YeeBadgeViewHeader.h"


@interface GQHHomeView () <UITableViewDelegate,UITableViewDataSource> {
    
    NSArray *iconArray;
    NSArray *titleArray;
    NSArray *captionArray;
    UIButton * messageButton;
}

@end


@implementation GQHHomeView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
        
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

- (void)setBadgeCount:(NSString *)badgeCount
{
    _badgeCount = badgeCount;
    
    if (badgeCount.intValue == 0)
    {
        messageButton.redDotNumber = 0;
    }
    else
    {
        messageButton.redDotText = badgeCount;
        messageButton.redDotColor = [UIColor redColor];
        messageButton.redDotTextColor = [UIColor whiteColor];
        messageButton.redDotTextFont = [UIFont systemFontOfSize:11];
        messageButton.redDotOffset = CGPointMake(-(messageButton.qh_width / 2 - 8), messageButton.qh_height / 2 - 8);
        [messageButton ShowBadgeView];
    }
}

#pragma mark --Data
- (void)loadLocalData {
    
    iconArray = @[@"home_order",@"home_freight",@"home_trace",@"home_freight",@"home_trace"];
    titleArray = @[@"运单管理",@"回程货源",@"我的报价",@"在途追踪",@"我的货源"];
    captionArray = @[@"快捷查询运单\n轻松完成各个节点管控",@"回程货源推荐\n方便快捷的查询回程货源",@"快速查看报价\n方便快捷的查询报价信息",@"在途实时跟踪\n让追踪货物运输安全无忧",@"快速查看货源\n方便快捷的查看货源信息"];
}

#pragma mark --View
- (void)loadUserInterface {
    
    UIView *headerView = [[UIView alloc] init];
    headerView.layer.contents = (id)[UIImage imageNamed:@"home_background_image"].CGImage;
    [self addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.and.left.and.right.mas_equalTo(self);
        make.height.mas_equalTo(0.8 * self.qh_width);
    }];
    
    messageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [messageButton setImage:[UIImage imageNamed:@"home_message"] forState:UIControlStateNormal];
    [messageButton addTarget:self action:@selector(checkNotification:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:messageButton];
    [messageButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(headerView.qh_statusBarHeight);
        make.left.mas_equalTo(headerView);
        make.size.mas_equalTo(CGSizeMake(44, 44));
    }];
    
    UIButton *customerServiceButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [customerServiceButton setImage:[UIImage imageNamed:@"home_customerservice"] forState:UIControlStateNormal];
    [customerServiceButton addTarget:self action:@selector(contactCustomerService:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:customerServiceButton];
    [customerServiceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(messageButton);
        make.right.mas_equalTo(headerView);
        make.size.mas_equalTo(messageButton);
    }];
    
    UIButton *postButton = [UIButton buttonWithType:UIButtonTypeCustom];
    postButton.backgroundColor = kRedColor;
    postButton.layer.cornerRadius = 5.0;
    postButton.layer.masksToBounds = YES;
    if ([USER_GET_TYPE isEqualToString:USER_TYPE_D]) {
        
        [postButton setTitle:@"发布空车" forState:UIControlStateNormal];
    } else {
        
        [postButton setTitle:@"发布货源" forState:UIControlStateNormal];
    }
    [postButton setTitleColor:kWhiteColor forState:UIControlStateNormal];
    [postButton addTarget:self action:@selector(post:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:postButton];
    [postButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.center.mas_equalTo(headerView);
        make.size.mas_equalTo(CGSizeMake(200, 60));
    }];
    
    _qh_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _qh_tableView.delegate = self;
    _qh_tableView.dataSource = self;
    _qh_tableView.backgroundColor = [UIColor whiteColor];
    _qh_tableView.scrollEnabled = YES;
    _qh_tableView.showsVerticalScrollIndicator = NO;
    _qh_tableView.tableFooterView = [UIView new];
    _qh_tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
    if (@available(iOS 11.0, *)) {
        
        _qh_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_qh_tableView];
    [_qh_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(headerView.mas_bottom);
        make.left.and.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self).with.inset(self.qh_tabBarHeight + self.qh_homeIndicatorHeight);
    }];
}

#pragma mark --Delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    /// 根据角色隐藏
    if ([USER_GET_TYPE isEqualToString:USER_TYPE_D]) {
        
        if (3 == indexPath.row || 4 == indexPath.row) {
            
            return CGFLOAT_MIN;
        }
    } else {
        
        if (1 == indexPath.row || 2 == indexPath.row || 5 == indexPath.row) {
            
            return CGFLOAT_MIN;
        }
    }
    
    return 100.0f;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return titleArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    cell.imageView.image = [UIImage imageNamed:iconArray[indexPath.row]];
    cell.textLabel.text = titleArray[indexPath.row];
    cell.detailTextLabel.text = captionArray[indexPath.row];
    cell.detailTextLabel.numberOfLines = 0;
    
    /// 根据角色隐藏
    if ([USER_GET_TYPE isEqualToString:USER_TYPE_D]) {
        
        if (3 == indexPath.row || 4 == indexPath.row) {
            
            cell.hidden = YES;
        }
    } else {
        
        if (1 == indexPath.row || 2 == indexPath.row || 5 == indexPath.row) {
            
            cell.hidden = YES;
        }
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if ([_qh_delegate respondsToSelector:@selector(qh_selectedRowWithTableView:atIndexPath:)]) {
        
        [_qh_delegate qh_selectedRowWithTableView:tableView atIndexPath:indexPath];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

- (IBAction)post:(UIButton *)sender {
    DLog(@"发布空车或货源信息");
    
    if ([_qh_delegate respondsToSelector:@selector(qh_provideSupplyOrFree:)]) {

        [_qh_delegate qh_provideSupplyOrFree:sender];
    }
}

- (IBAction)checkNotification:(UIButton *)sender {
    DLog(@"查看推送通知");
    
    if ([_qh_delegate respondsToSelector:@selector(qh_checkNotification:)]) {
        
        [_qh_delegate qh_checkNotification:sender];
    }
}

- (IBAction)contactCustomerService:(UIButton *)sender {
    DLog(@"联系客服");
    
    if ([_qh_delegate respondsToSelector:@selector(qh_contactCustomerService:)]) {
        
        [_qh_delegate qh_contactCustomerService:sender];
    }
}

@end
